package com.lw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

/**
 * 主窗口
 */
public class MyFrame extends JFrame implements KeyListener,Runnable{

    //存储所有背景
    private java.util.List<Background> allBg = new ArrayList<>();

    //当前背景
    private Background nowBg = new Background();

    private Mario mario = new Mario();//马里奥

    //解决屏幕闪烁
    private Image offScreenImage = null;

    //实现马里奥的运动
    private Thread thread = new Thread(this);


    public MyFrame(){
        this.setSize(StaticValue.WIDTH,StaticValue.HEIGHT);
        this.setLocationRelativeTo(null);//居中
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口，结束程序
        this.setResizable(false);
        this.setTitle("超级玛丽");

        //键盘事件
        this.addKeyListener(this);

        StaticValue.init();//初始化常量信息

        mario = new Mario(10,350);


        //创建全部场景
        for (int i = 1; i <= 3; i++) {
            allBg.add(new Background(i,i==3? true:false));
        }
        nowBg = allBg.get(0);
        mario.setBackground(nowBg);

        repaint();//绘制图像

        thread.start();

        new Music();

    }

    @Override
    public void paint(Graphics g) {
        if(offScreenImage==null) offScreenImage = createImage(StaticValue.WIDTH,StaticValue.HEIGHT);

        Graphics imgG = offScreenImage.getGraphics();
        imgG.fillRect(0,0,StaticValue.WIDTH,StaticValue.HEIGHT);

        //绘制背景
        imgG.drawImage(nowBg.getBgImage() ,0,0,StaticValue.WIDTH,StaticValue.HEIGHT,this);

        //绘制障碍物
        for (Obstacle obstacle : nowBg.getObstacleList()) {
            imgG.drawImage(obstacle.getShow(),obstacle.getX(),obstacle.getY(),this);
        }

        //绘制马里奥
        imgG.drawImage(mario.getShow(),mario.getX(),mario.getY(),100,100,this);

        g.drawImage(offScreenImage,0,0,this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT){//左移  键盘方向键
            mario.leftMove();
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            mario.rightMove();
        }else if(e.getKeyCode()==KeyEvent.VK_UP){
            mario.jump();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_LEFT){//键盘方向键
            mario.leftStop();
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            mario.rightStop();
        }
    }

    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }

    @Override
    public void run() {
        while (true){
            repaint();

            //朝右边跑到下一个场景了
            if(mario.getX()>=StaticValue.WIDTH-100){
                if(nowBg.getSort()<3){
                    nowBg = allBg.get(nowBg.getSort());
                    mario.setBackground(nowBg);
//                    马里奥到了下一个场景，要切换到最左边，因为是从右边跑过去的
                    mario.setX(10);
                    mario.setY(350);
                }else{
                    //最后一个场景，不允许跑到右侧屏幕外
                    if(mario.getX()>StaticValue.WIDTH-100){
                        mario.setX(StaticValue.WIDTH-100);
                    }
                }

            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
